/**
 * Create in 2012-10-12 下午8:05:22.
 */
package kit4j.app.notepad.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.text.DefaultStyledDocument;

import kit4j.app.notepad.config.NotepadConfig;
import kit4j.app.notepad.ui.comp.MinimizeNotepadLabel;
import kit4j.app.notepad.ui.comp.ResizeWindowLabel;
import kit4j.app.notepad.ui.listener.NotepadCloseActionListener;
import kit4j.app.notepad.ui.mediator.NotepadMenuMediator;
import kit4j.app.notepad.ui.panel.BorderMenuPanel;
import kit4j.app.notepad.ui.panel.NotepadBorderPanel;
import kit4j.util.swing.SwingUtil;
import kit4j.view.cs.exception.ListNoSelectionException;
import kit4j.view.cs.swing.dialog.JFontChooser;

/**
 * 
 * @author MiniKnife
 * 
 */
public class Notepad extends JDialog {

	private static final long serialVersionUID = 1L;

	private final NotepadConfig config;
	private NotepadBorderPanel contentPane;
	private NotepadMenuMediator menuMediator;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JFontChooser fontChooser;
	private Thread weightCompInitThread;

	public Notepad(NotepadConfig config) {
		super();
		this.config = config;
		initNotepad();
		initMenu();
		initComponent();
		initInteraction();
	}

	private void initNotepad() {
		setAlwaysOnTop(true);
		setUndecorated(true);
		SwingUtil.setWindowOpaque(this, false);
		initContentPane();
	}
	
	private void initContentPane() {
		contentPane = new NotepadBorderPanel();
		menuMediator = new NotepadMenuMediator();
		menuMediator.initMediator(this, contentPane, NotepadMenuMediator.MENU_STATE_HIDING);
		contentPane.setBackground(Color.green);
		setContentPane(contentPane);
	}
	
	private void initMenu() {
		MinimizeNotepadLabel minimizeLabel = new MinimizeNotepadLabel(this);
		minimizeLabel.setText(config.getString("NOTEPAD_BTN_MIN_LABEL", "  ─  "));
		minimizeLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		
		JLabel closeBtn = new JLabel(config.getString("NOTEPAD_BTN_CLOSE_LABEL", "  ╳  "), config.getIcon("NOTEPAD.CLOSE_ICON"), SwingConstants.CENTER);
		closeBtn.setVerticalTextPosition(SwingConstants.CENTER);
		closeBtn.setBorder(BorderFactory.createLineBorder(Color.black));
		closeBtn.addMouseListener(new NotepadCloseActionListener(this));
		
		BorderMenuPanel topPanel = contentPane.getTopPanel();
		topPanel.setBackground(Color.yellow);
		topPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 0));
		topPanel.add(minimizeLabel);
		topPanel.add(closeBtn);
		
		ResizeWindowLabel resizeLabel = new ResizeWindowLabel(config.getString("NOTEPAD_BTN_RESIZE_LABEL", "resize"));
		
		BorderMenuPanel bottomPanel = contentPane.getBottomPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 1, 0));
		bottomPanel.add(resizeLabel);
	}
	
	private void initComponent() {
		weightCompInitThread = new Thread() {
			public void run() {
				fontChooser = new JFontChooser(Notepad.this, "设置字体");
				fontChooser.doLayout();
				fontChooser.validate();
			}
		};
		weightCompInitThread.start();
		textArea = new JTextArea(new DefaultStyledDocument());
		textArea.setMargin(new Insets(3, 4, 3, 3));
		textArea.setTabSize(4);
		scrollPane = new JScrollPane(textArea);

		setLayout(new BorderLayout());
		add(scrollPane);
	}

	private void initInteraction() {
		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.isControlDown() && KeyEvent.VK_ENTER == evt.getKeyCode()) {
					Notepad.this.setAlwaysOnTop(!Notepad.this.isAlwaysOnTop());
				} else if (evt.isControlDown() && KeyEvent.VK_L == evt.getKeyCode()) {
					textArea.setLineWrap(!textArea.getLineWrap());
				}
			}
		});

		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.isShiftDown() && KeyEvent.VK_ENTER == evt.getKeyCode()) {
					try {
						weightCompInitThread.join(10000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
						weightCompInitThread.interrupt();
						weightCompInitThread.run();
					}
					fontChooser.setSelectedFont(textArea.getFont());
					if (fontChooser.showChooseDialog() == JOptionPane.OK_OPTION) {
						try {
							textArea.setFont(fontChooser.getSelectedFont());
						} catch (ListNoSelectionException e) {
						}
					}
				}
			}
		});
	}

	public JTextArea getTextArea() {
		return textArea;
	}

	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}
	
	public void clean() {
		textArea.setText("");
		menuMediator.menuAct(NotepadMenuMediator.MENU_STATE_HIDING);
	}

}
